//
//  LXExtension+NSString.swift
//  LXExtensions
//
//  Created by yoctech on 2021/10/8.
//

import Foundation
import CommonCrypto

@objc public extension NSString {
    
    // MARK: 字符串转换
    
    /// 字符串转日期
    /// - Parameter dateFormat: 日期格式
    /// - Returns: 目标日期
    @objc func lx_date(_ dateFormat: String = "yyyy-MM-dd") -> Date? {
        return (self as String).lx_date(dateFormat)
    }
    
    /// 随机字母字符串生成,26字母,包含大小写
    /// - Parameter length: 字符串长度
    /// - Returns: 随机字符串
    @objc static func lx_random(length:Int) -> String {
        return String.lx_random(length: length)
    }
    
    /// 从json文件里面读取字符串
    /// - Parameter fileName: json文件名,不包含.json后缀
    /// - Returns: 结果字符串,如果读取失败则返回空字符串
    @objc static func lx_fromJsonFile(_ fileName :String) -> String {
        return String.lx_fromJsonFile(fileName)
    }
    
    /// 字符串转json对象
    /// - Returns: Any对象,需要转换成Dictionary或者Array使用
    @objc func lx_toJsonObject() -> Any? {
        return (self as String).lx_toJsonObject()
    }
    
    /// base64编码
    @objc func lx_toBase64() -> String? {
        return (self as String).lx_toBase64()
    }

    /// base64解码
    @objc func lx_fromBase64() -> String? {
        return (self as String).lx_fromBase64()
    }
    
    /// MD5编码
    /// - Parameter isLowercase: 是否全部小写, 默认是
    /// - Returns: MD5字符串
    @objc func lx_toMd5(_ isLowercase : Bool = true) -> String? {
        return (self as String).lx_toMd5(isLowercase)
    }

    
    /// 转换成二维码
    /// - Returns: 目标图片
    @objc func toQRCode() -> UIImage? {
        return (self as String).toQRCode()
    }
    
    //MARK:- NSAttributedString相关方法
    
    /// 转换成NSAttributedString格式的HTML富文本
    /// - Returns: 结果NSAttributedString
    @objc func lx_toHtmlAttributed() -> NSAttributedString{
        return (self as String).lx_toHtmlAttributed()
    }

    
    /// 转换成NSAttributedString
    /// - Parameters:
    ///   - color: 文字颜色
    ///   - font: 文字字体
    /// - Returns: 结果NSAttributedString
    @objc func lx_toAttributed(color:UIColor? = nil, font: UIFont? = nil) -> NSAttributedString{
        return (self as String).lx_toAttributed(color: color, font: font)
    }
    
    /// 转换成NSAttributedString格式的图片附件
    /// - Parameter font: 需要的字体大小
    /// - Returns: 结果NSAttributedString
    @objc func lx_toImageAttributed(_ font: UIFont) -> NSAttributedString{
        return (self as String).lx_toImageAttributed(font)
    }
    
    /// 给字符串设置行间距,需要转换成NSAttributedString
    /// - Parameter space: 行间距
    /// - Returns: 结果NSAttributedString
    @objc func lx_lineSpace(_ space: Float) -> NSAttributedString{
        return (self as String).lx_lineSpace(space)
    }
    
    
    /// 根据固定宽度计算字符串所需高度
    /// - Parameters:
    ///   - width: 固定宽度
    ///   - font: 字体大小
    /// - Returns: 所需高度
    @objc func lx_needHeight(width: CGFloat, font:UIFont) -> CGFloat {
        let attributes = [NSAttributedString.Key.font : font]
        let size = self.boundingRect(with: CGSize.init(width: width, height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil).size
        return size.height
    }
    
    /// 根据固定高度计算字符串所需宽度
    /// - Parameters:
    ///   - width: 固定高度
    ///   - font: 字体大小
    /// - Returns: 所需宽度
    @objc func lx_needWidth(height: CGFloat, font:UIFont) -> CGFloat {
        let attributes = [NSAttributedString.Key.font : font]
        let size = self.boundingRect(with: CGSize.init(width: CGFloat(MAXFLOAT), height: height), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil).size
        return size.width
    }

}
